package com.springmvc.demo;

import com.springmvc.util.GsonUtils;
import com.springmvc.util.HttpUtil;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddressDemo {
    public static void main(String[] args)
    {

        // 官网获取的 API Key 更新为你注册的
        String clientId = "6EcV9G06yPy5zDLGMH0v0aAq";
        // 官网获取的 Secret Key 更新为你注册的
        String clientSecret = "x0UliIzoOweZ7zSHmcD9hq36bZHIey6F";

        String access_token=getAuth(clientId, clientSecret);
        //调用地址识别
        String request_url="https://aip.baidubce.com/rpc/2.0/nlp/v1/address";
        String url=request_url+"?access_token="+access_token;

        long startTime = System.currentTimeMillis(); //程序开始记录时间

        String text="福建省宁德市福安市城阳镇秦溪利洋村28号";
        int confidence=80;
        HashMap<String, Object> param=new HashMap<String, Object>();
        param.put("text", text);
        param.put("confidence", confidence);
        String params = GsonUtils.toJson(param);


        try {
            String result = HttpUtil.post(url, access_token, "application/json", params);
            long endTime = System.currentTimeMillis(); //程序结束记录时间
            long TotalTime = endTime - startTime; //总消耗时间
            System.out.println("time cost:"+TotalTime+"ms");
            System.out.println(result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    //获取access_token
    public static String getAuth(String ak, String sk)
    {
        // 获取token地址
        String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
        String getAccessTokenUrl = authHost
                // 1. grant_type为固定参数
                + "grant_type=client_credentials"
                // 2. 官网获取的 API Key
                + "&client_id=" + ak
                // 3. 官网获取的 Secret Key
                + "&client_secret=" + sk;
        try {
            URL realUrl = new URL(getAccessTokenUrl);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.err.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String result = "";
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            System.err.println("result:" + result);
            org.json.JSONObject jsonObject = new JSONObject(result);
            String access_token = jsonObject.getString("access_token");
            return access_token;
        } catch (Exception e) {
            System.err.printf("获取token失败！");
            e.printStackTrace(System.err);
        }
        return null;
    }
}